package com.playtech.tournament.balancing.test.client;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class TableStub extends Composite {
	FlexTable ft = new FlexTable();
	FlowPanel fp = new FlowPanel();
	Seat[] seat;
	ArrayList<Player> players = new ArrayList<Player>();
	Integer seats;
	String name;
	ListBox lb = new ListBox(true);
	Button balance = new Button("Balance");
	Label tableName = new Label();
	Boolean gameIsPaused = true;
	Boolean gameIsPausedWithSinglePlayer = false;
	Label gameIsPausedLabel = new Label("Game paused");
	
	public String getName() {
		return name;
	}

	public TableStub(String tName, Integer seats, int playersNumber) {
		this.seats = seats;
		seat = new Seat[seats];
		for (int i = 0; i < seats; i++) {
			Player player = null;
			if (playersNumber > 0) {
				player = new Player(tName + " | " + playersNumber);
				players.add(player);
				lb.addItem(player.getName());
				playersNumber--;
			}
			seat[i] = new Seat(player);
			ft.setWidget(i > 4 ? 1 : 0, i > 4 ? (i - 5) : i, seat[i]);

		}
		tableName.setText("Table:" + tName);
		fp.add(tableName);
		this.name = tName;
		fp.add(ft);
		fp.add(lb);
		fp.add(balance);
		lb.setVisibleItemCount(4);
		fp.add(gameIsPausedLabel);

		balance.addClickHandler(ch);
		initWidget(fp);
		setStyleName("float");
	}

	ClickHandler ch = new ClickHandler() {

		public void onClick(ClickEvent event) {
			ArrayList<Player> toRemove = new ArrayList<Player>();
			for (int item = 0; item < lb.getItemCount(); item++) {
				if (lb.isItemSelected(item)) {
					String name = lb.getItemText(item);
					Seat seat = findSeatByName(name);
					Player player = seat.getPlayer();

					players.remove(player);
					toRemove.add(player);
					Balancing.log("Removed:" + player.getName());
					seat.removePlayer();
				}
			}
			for (Player player : toRemove)
				removeFromList(player);
			
			if (countPlayers() < 2) 
			{
				pauseGame();
			}
			
			Balancing.checkBalanceTables(TableStub.this);
		}
	};

	public int countPlayers() {
		return players.size();
	}

	public Player removeFirstPlayer() {
		for (int i = 0; i < seats; i++) {
			try {

				if (seat[i].hasPlayer()) {
					Player player = seat[i].getPlayer();
					seat[i].removePlayer();
					players.remove(player);
					removeFromList(player);
					return player;
				}
			} catch (Exception e) {

			}
		}
		return null;
	}

	public void seatPlayer(Player player, boolean bb) {
		for (int i = 0; i < seats; i++) {
			if (!seat[i].hasPlayer()) {
				seat[i].seatPlayer(player);
				players.add(player);
				ft.setWidget(i > 4 ? 1 : 0, i > 4 ? (i - 5) : i, seat[i]);
				lb.addItem(player.getName());
				return;
			}
		}

	}
	
	public void startGame()
	{
		gameIsPaused = false;	
		gameIsPausedWithSinglePlayer = false;
		gameIsPausedLabel.setVisible(false);
	}
	
	public void pauseGame()
	{
		gameIsPaused = true;
		gameIsPausedWithSinglePlayer = true;
		gameIsPausedLabel.setVisible(true);
	}
	
	public boolean getIsPaused()
	{
		return gameIsPaused;
	}
	
	public boolean getIsPausedWithSinglePlayer()
	{
		return gameIsPausedWithSinglePlayer;
	}

	public void close() {
		getElement().getStyle().setBackgroundColor("grey");
		fp.remove(lb);
		fp.remove(balance);
		tableName.setText(tableName.getText() + " - Game stopped");
	}

	private void removeFromList(Player player) {
		for (int item = 0; item < lb.getItemCount(); item++) {
			if (lb.getItemText(item).equals(player.getName())) {
				lb.removeItem(item);
				break;
			}
		}
	}

	private Seat findSeatByName(String name) {
		for (int i = 0; i < seats; i++) {
			Player player = seat[i].getPlayer();
			if (player != null && player.getName().equals(name)) {
				return seat[i];
			}
		}
		
		return null;
	}
}
